import execjs
import requests
import warnings


warnings.filterwarnings("ignore")

headers = {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
    "cache-control": "no-cache",
    "pragma": "no-cache",
    "priority": "u=1",
    "referer": "https://www.shanghairanking.cn/institution",
    "sec-ch-ua": "\"Google Chrome\";v=\"141\", \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"141\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "script",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
    "Cookie": "Hm_lvt_af1fda4748dacbd3ee2e3a69c3496570=1759889332; HMACCOUNT=52659E05847387B9; _clck=1ge184j%5E2%5Efzz%5E0%5E2107; Hm_lpvt_af1fda4748dacbd3ee2e3a69c3496570=1759889435; _clsk=vj9yta%5E1759889436641%5E2%5E1%5Ek.clarity.ms%2Fcollect"
}
req_url = 'https://www.shanghairanking.cn/_nuxt/static/1758793770/institution/payload.js'
params = {}

req = requests.get(req_url, headers=headers, params=params, verify=False)
req.encoding = 'utf-8'
html = req.text
script = html.replace('__NUXT_JSONP__("/institution",', 'function getData(){ return ')
script = script.replace(')));', '))};')
ctx = execjs.compile(script)
res = ctx.call('getData')
univData = res.get('data')[0].get('univList')
for idx, item in enumerate(univData):
    nameCn = item.get('nameCn')
    provinceShort = item.get('provinceShort')
    adminType = item.get('adminType')
    categoryName = item.get('categoryName')
    tags = item.get('tags')
    if tags is None:
        tags = []
    tags = ' '.join(tags)
    print(idx + 1, nameCn, provinceShort, adminType, categoryName, tags)
